package cn.lic.tool.poi.excel.function;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.util.TypeUtils;

/**
 * 直接量
 * @author Administrator
 *
 */
public class SValue extends IValue {
	
	public SValue(Object value) {
		this.value = value;
	}

	@Override
	public <T> T castValue(Type type) {
		Object val = null;
		if (this.value == null) {
			return null;
		}
		switch (type) {
			case OBJECT:
				val = value;
				break;
			case INT:
				val = TypeUtils.castToInt(value);
				break;
			case FLOAT:
				val = TypeUtils.castToFloat(value);
				break;
			case STRING:
				val = TypeUtils.castToString(value);
				break;
			case BOOL:
				val = TypeUtils.castToBoolean(value);
				break;
			case BIG_DECIMAL:
				val = TypeUtils.castToBigDecimal(value);
				break;
			case JSON_ARRAY:
				if (value instanceof JSONArray) {
					val = value;
				} else {
					val = JSON.toJSON(value);
				}
				break;
		default:
			val = value;
		}
		return (T)val;
	}

	@Override
	public boolean isNull() {
		return this.value == null;
	}
	
	@Override
	public String toString() {
		return "S:" + JSON.toJSONString(value);
	}
}
